﻿from django.http import HttpResponse, HttpResponseRedirect
from django.core.paginator import ObjectPaginator, InvalidPage
from app.engine import models
from app.engine import bforms
from app.forum import models as datastores
from google.appengine.api import users
from google.appengine.ext import db
from app.functions.funcs import render,split_tags,formate_time,gen_duration
import xml.etree.ElementTree as et
from app.functions import gviz_api
from app.base.auth import LoginUrl,ChkAuth,ChkAdmin
from google.appengine.api import memcache

def greateCache():
    players = models.Players.all()
    events = models.EventData.all()
    pievents = models.PlayersInEvent.all()
    actions = models.ActionsInEvent.all()
    gears = models.GearData.all()
    memcache.set_multi({'cache_players':players,
               'cache_events':events,
               'cache_pievents':pievents,
               'cache_actions':actions,
               'cache_gears':gears}, time=3600)

def get_score(self,data_type,actions_type):
    score = 0
    for action in memcache.get(data_type).filter('cat =',actions_type):
       if self.key() in action.players:
           score = action.score + score
    return score

def get_costs(self):
    cost_in_items=0
    gears = memcache.get('cache_gears').filter('gear_player =',self)
    for gear in gears:
        cost_in_items = gear.gear_costs + cost_in_items
    return cost_in_items

def get_attendance(self):
    events_count = memcache.get('cache_events').count()
    attendance_count = 0
    rate=0
    for event in memcache.get('cache_pievents'):
        if self.key() in event.players:
            attendance_count += 1.0
    rate = attendance_count / events_count * 100
    return rate

def dkpTable():
    data = []
    for player in memcache.get('cache_players').fetch(40):
        earned_score = get_score(player,'cache_actions','normal')
        rewards_score = get_score(player,'cache_actions','reward')
        punishment_score = get_score(player,'cache_actions','punishment')
        adjust_score = get_score(player,'cache_actions','adjust')
        cost_in_items = get_costs(player)
        current = earned_score + rewards_score + adjust_score - punishment_score - cost_in_items
        rate = get_attendance(player)
        player_dict = {"name":player.player_name,
                       "class":player.player_class,
                       "earned":earned_score,
                       "adjust":adjust_score,
                       "rewards":rewards_score,
                       "punishment":punishment_score,
                       "cost":cost_in_items,
                       "current":current,
                       "rate":rate,
                       }

        data.append(player_dict)
    
    memcache.set('player_score_data',data,time=3600)

def pubEventView(request):
    events = models.EventData.all().order('-post_on')
    payload = dict(events=events)
    return render('index.html', payload)

def pubEventInfo(request,event_id):
    event = models.EventData.get_by_id(int(event_id))
    all_players_in_event = models.PlayersInEvent.all().filter('event =',event)
    actions = models.ActionsInEvent.all().filter('event =',event).filter('cat =','normal')
    actions_num = actions.count()
    loots = models.GearData.all().filter('event =',event)
    loots_num = loots.count()
    rewards = models.ActionsInEvent.all().filter('event =',event).filter('cat =','reward')
    rewards_num = rewards.count()
    punishment = models.ActionsInEvent.all().filter('event =',event).filter('cat =','punishment')
    punishment_num = punishment.count()
    players = models.Players.get(all_players_in_event[0].players)
    payload = dict(event=event,
                    players=players,
                    loots=loots,
                    eventid=event_id,
                    actions=actions,
                    rewards=rewards,
                    punishment=punishment,
                    actions_num=actions_num,
                    loots_num=loots_num,
                    rewards_num=rewards_num,
                    punishment_num=punishment_num)
    return render('pubEventInfo.html',payload)

def pubPlayerInfo(request,player_id):
    player = models.Players.get_by_id(int(player_id))
    gears = models.GearData.all().filter('gear_player =',player)
    player_in_events = []
    for event in models.PlayersInEvent.all():
        if player.key() in event.players:
            player_in_events.append(event)
    payload = dict(player=player,gears=gears,events=player_in_events)
    return render('PlayerInfo.html', payload)

def adminImportEvent(request):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        if request.method == 'POST':
            eventform = bforms.EventForm(request.POST)
            if eventform.is_valid():
                eventform.save()
                return HttpResponseRedirect('/admin/events')
        else:
            eventform = bforms.EventForm()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(eventform = eventform)
    return render('AdminImportEvent.html', payload)

def adminListEvent(request):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        events = models.EventData.all()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(events = events)
    return render('AdminEventList.html', payload)

def adminEditEvent(request,event_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        event = models.EventData.get_by_id(int(event_id))
        eventform = bforms.EventForm(initial={'title':event.title,
                                              'summary':event.summary,
                                              'content':event.content,
                                              'slug':event.slug,
                                              'start':event.start,
                                              'end':event.end,
                                              'realm':event.realm})
        if request.method == 'POST':
            eventform = bforms.EventForm(request.POST)
            if eventform.is_valid():
                eventform.edit(event)
                return HttpResponseRedirect('/admin/events')
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(eventform=eventform)
    return render('AdminImportEvent.html',payload)

def adminDeleteEvent(request,event_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        event = models.EventData.get_by_id(int(event_id))
        gears_in_event = models.GearData.all().filter('event =',event.key())
        actions = models.ActionsInEvent.all().filter('event =',event.key())
        for action in actions:
            action.delete()
        for gear in gears_in_event:
            gear.delete()
        event.delete()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    return HttpResponseRedirect('/admin/events')

def adminGenEvent(request,event_id):
    event = models.EventData.get_by_id(int(event_id))
    tree = et.fromstring(event.content.encode('utf8'))
    event.start = formate_time(tree.find("start").text)
    event.end = formate_time(tree.find("end").text)
    event.realm = tree.find("realm").text
    event.put()
    all_players = tree.find("PlayerInfos")
    players = all_players.getchildren()
    players_in_event = []
    for player in players:
        query = models.Players.all()
        try:
            player_name=player.find('name').text
        except AttributeError:
            player_name=None
        try:
            player_race=player.find('race').text
        except AttributeError:
            player_race=None
        try:
            player_level=player.find('level').text
        except AttributeError:
            player_level=None
        try:
            player_sex=player.find('sex').text
        except AttributeError:
            player_sex=None
        try:
            player_class=player.find('class').text
        except AttributeError:
            player_class=None
        try:
            player_guild=player.find('guild').text
        except AttributeError:
            player_guild=None

        player_template=models.Players(player_name=player_name,
                                       player_race=player_race,
                                       player_level=player_level,
                                       player_sex=player_sex,
                                       player_class=player_class,
                                       player_guild=player_guild)
        if query.filter('player_name =', player_template.player_name).count() == 0:
            player_template.put()
            players_in_event.append(player_template.key())
        else:
            lquery=query.filter('player_name =',player_template.player_name)
            players_in_event.append(lquery[0].key())

    players_event_data = models.PlayersInEvent(event=event,players=players_in_event)
    players_event_data.put()

    all_boss = tree.find("BossKills")
    bosskills = all_boss.getchildren()
    for bosskilled in bosskills:
        try:
            boss_name = bosskilled.find('name').text
        except AttributeError:
            boss_name = None
        boss_action = models.ActionsInEvent(title=boss_name,event=event,cat='normal')
        boss_action.put()

    all_loots = tree.find("Loot")
    loots = all_loots.getchildren()
    for loot in loots:
        try:
            loot_name=loot.find('ItemName').text
        except AttributeError:
            loot_name=None
        try:
            loot_id=loot.find('ItemID').text
        except AttributeError:
            loot_id=None
        try:
            loot_icon=loot.find('Icon').text
        except AttributeError:
            loot_icon=None
        try:
            loot_class=loot.find('Class').text
        except AttributeError:
            loot_class=None
        try:
            loot_subclass=loot.find('SubClass').text
        except AttributeError:
            loot_subclass=None
        try:
            loot_color=loot.find('Color').text
        except AttributeError:
            loot_color="#999"
        try:
            loot_count=int(loot.find('Count').text)
        except AttributeError:
            loot_count=0
        try:
            loot_player=loot.find('Player').text
        except AttributeError:
            loot_player=None
        try:
            loot_costs=int(loot.find('Costs').text)
        except AttributeError:
            loot_costs=0
        try:
            loot_time=loot.find('Time').text
        except AttributeError:
            loot_time=None
        try:
            loot_zone=loot.find('Zone').text
        except AttributeError:
            loot_zone=None
        try:
            loot_boss=loot.find('Boss').text
        except AttributeError:
            loot_boss=None
        try:
            loot_tooltip=loot.find('tooltip').text
        except AttributeError:
            loot_tooltip=None
        try:
            loot_note=loot.find('Note').text
        except AttributeError:
            loot_note=None
        
        if loot_player is not None:
            lquery=models.Players.all()
            lplayers=lquery.filter('player_name =',loot_player)
            if lplayers.count()==0:
                splayer=model.Players(player_name=loot_player)
                splayer.put()
                uplayers=uquery.filter('player_name =',loot_player)
                zloot=uplayers[0].key()

            else:
                  zloot=lplayers[0].key()
        elif loot_player is None:
            zloot=None
        geardata=models.GearData(event=event,
                                 gear_name=loot_name,
                                 gear_id=loot_id,
                                 gear_icon=loot_icon,
                                 gear_class=loot_class,
                                 gear_subclass=loot_subclass,
                                 gear_color=loot_color,
                                 gear_count=loot_count,
                                 gear_player=zloot,
                                 gear_costs=loot_costs,
                                 gear_time=loot_time,
                                 gear_zone=loot_zone,
                                 gear_boss=loot_boss,
                                 gear_tooltip=loot_tooltip,
                                 gear_note=loot_note)
        geardata.put()
    return HttpResponseRedirect('/admin/events/%s' %event_id)

def adminEditGear(request,gear_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        gear = models.GearData.get_by_id(int(gear_id))
        if request.method == 'POST':
            gearform = bforms.GearForm(request.POST)
            if gearform.is_valid():
                gearform.edit(gear)
                return HttpResponseRedirect('/admin/loots')
        else :
            gearform = bforms.GearForm(initial={'event':gear.event,
                                                'gear_name':gear.gear_name,
                                                'gear_id':gear.gear_id,
                                                'gear_icon':gear.gear_icon,
                                                'gear_class':gear.gear_class,
                                                'gear_subclass':gear.gear_subclass,
                                                'gear_color':gear.gear_color,
                                                'gear_count':gear.gear_count,
                                                'gear_player':gear.gear_player,
                                                'gear_costs':gear.gear_costs,
                                                'gear_time':gear.gear_time,
                                                'gear_zone':gear.gear_zone,
                                                'gear_boss':gear.gear_boss,
                                                'gear_tooltip':gear.gear_tooltip,
                                                'gear_note':gear.gear_note})
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(gearform=gearform)
    return render('AdminGearForm.html',payload)


def adminDeleteGear(request,gear_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        gear = models.GearData.get_by_id(int(gear_id))
        updatePlayerScore(gear.gear_player,'cost',-gear.gear_costs)
        gear.delete()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    return HttpResponseRedirect('/admin/loots')

def adminListGears(request):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        all_gears = models.GearData.all()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload=dict(loots=all_gears)
    return render('AdminLootsList.html',payload)

def adminListPlayers(request,page=1,orderby=None):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        try:
            page = int(page)-1
        except:
            page = 0
        show_prev = False
        show_next = False
        all_players = models.Players.all()
        paginator = ObjectPaginator(all_players,24)
        if page >= paginator.pages:
            page = paginator.pages - 1
        players=paginator.get_page(page)
        pages=range(1,paginator.pages+1)
        show_prev = paginator.has_previous_page(page)
        show_next = paginator.has_next_page(page)
        page=page+1
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(players=players,
                   show_prev=show_prev,
                   show_next=show_next,
                   page=page,
                   pages=pages,
                   prev=page-1,
                   next=page+1)
    return render('AdminPlayers.html',payload)

def adminEditPlayer(request,player_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        player = models.Players.get_by_id(int(player_id))
        if request.method == 'POST':
            playerform = bforms.PlayerForm(request.POST)
            if playerform.is_valid():
                playerform.edit(player)
                return HttpResponseRedirect('/admin/players')
        else:
            playerform = bforms.PlayerForm(initial={'player_name':player.player_name,
                                                    'player_class':player.player_class,
                                                    'player_race':player.player_race,
                                                    'player_guild':player.player_guild,
                                                    'player_sex':player.player_sex,
                                                    'player_level':player.player_level})
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(playerform=playerform)
    return render('AdminPlayersForm.html',payload)

def adminDeletePlayer(request,player_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        player = models.Players.get_by_id(int(player_id))
        player.delete()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    return HttpResponseRedirect('/admin/players')

def adminEventForm(request,event_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        event = models.EventData.get_by_id(int(event_id))
        all_players_in_event = models.PlayersInEvent.all().filter('event =',event)
        actions = models.ActionsInEvent.all().filter('event =',event).filter('cat =','normal')
        loots = models.GearData.all().filter('event =',event)
        rewards = models.ActionsInEvent.all().filter('event =',event).filter('cat =','reward')
        punishment = models.ActionsInEvent.all().filter('event =',event).filter('cat =','punishment')
        players = models.Players.get(all_players_in_event[0].players)
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(event=event,players=players,loots=loots,eventid=event_id,actions=actions,rewards=rewards,punishment=punishment)
    return render('EventView.html',payload)

def adminAddGear(request, event_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        event = models.EventData.get_by_id(int(event_id))
        if request.method == 'POST':
            gearform = bforms.GearForm(request.POST)
            if gearform.is_valid():
                gearform.save(gear,event)
                return HttpResponseRedirect('/admin/loots')
        else:
            gearform = bforms.GearForm()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(gearform=gearform)
    return render('AdminGearForm.html',payload)

def adminChoosePlayers(request, event_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        event = models.EventData.get_by_id(int(event_id))
        players_in_event = models.PlayersInEvent.all().filter('event =',event)
        players_in_event = players_in_event[0]
        players_list = models.Players.get(players_in_event.players)
        players = models.Players.all().order('player_class')
        all_players=[]
        for player in players:
            if player.key() in players_in_event.players:
                player.checkbox = True
            else:
                player.checkbox = False
            all_players.append(player)
        if request.method == 'POST':
            choosed_players_key = []
            post_players = request.POST.getlist('player')
            choosed_players_in_event = models.Players.get(post_players)
            for player in choosed_players_in_event:
                choosed_players_key.append(player.key())
                players_in_event.players = choosed_players_key
            players_in_event.put()
            return HttpResponseRedirect('/admin/players/choose/%s/' %event_id)
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(players=all_players)
    return render('AdminChoosePlayers.html', payload)

def adminAddActions(request,event_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        event = models.EventData.get_by_id(int(event_id))
        players_in_event = models.PlayersInEvent.all().filter('event =',event)
        players_in_event = players_in_event[0]
        players = models.Players.get(players_in_event.players)
        if request.method == 'POST':
            members = []
            action_title = request.POST.get('title')
            action_cat = request.POST.get('cat')
            action_summary = request.POST.get('summary')
            action_score = request.POST.get('score')
            members_list = request.POST.getlist('player')
            members_list_obj = models.Players.get(members_list)
            for player in members_list_obj:
                members.append(player.key())
                if action_cat is 'punishment':
                    updatePlayerScore(player,'punishment',action_score)
                elif action_cat is 'reward':
                    updatePlayerScore(player,'reward',action_score)
                elif action_cat is 'normal':
                    updatePlayerScore(player,'earn',action_score)
                elif action_cat is 'adjust':
                    updatePlayerScore(player,'adjust',action_score)
            action = models.ActionsInEvent(event = event,
                                           title = action_title.decode('utf-8'),
                                           cat = action_cat,
                                           summary = action_summary.decode('utf-8'),
                                           score = int(action_score),
                                           players = members)
            action.put()
            return HttpResponseRedirect('/admin/events/%s/' %event.key().id())
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(players=players)
    return render('AdminActions.html', payload)

def adminEditActions(request,action_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        action = models.ActionsInEvent.get_by_id(int(action_id))
        event = models.EventData.get(action.event.key())
        players_in_event = models.PlayersInEvent.all().filter('event =',event)
        players = models.Players.get(players_in_event[0].players)
        players_obj = []
        for player in players:
            if player.key() in action.players:
                player.checkbox = True
            else:
                player.checkbox = False
            players_obj.append(player)
    
        if request.method == 'POST':
            members = []
            action_title = request.POST.get('title')
            action_cat = request.POST.get('cat')
            action_summary = request.POST.get('summary')
            action_score = int(request.POST.get('score'))
            members_list = request.POST.getlist('player')
            members_list_obj = models.Players.get(members_list)
            update_score = action_score - action.score
            for player in members_list_obj:
                members.append(player.key())
                if action_cat is 'punishment':
                    updatePlayerScore(player,'punishment',update_score)
                elif action_cat is 'reward':
                    updatePlayerScore(player,'reward',update_score)
                elif action_cat is 'normal':
                    updatePlayerScore(player,'earn',update_score)
                elif action_cat is 'adjust':
                    updatePlayerScore(player,'adjust',update_score)
            action.title = action_title.decode('utf-8')
            action.cat = action_cat
            action.summary = action_summary.decode('utf-8')
            action.score = action_score
            action.players = members
            action.put()
            return HttpResponseRedirect('/admin/events/%s/' %event.key().id())
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict(players=players_obj,action=action)
    return render('AdminEditAction.html', payload)

def adminDeleteActions(request, action_id):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        action = models.ActionsInEvent.get_by_id(int(action_id))
        for player in action.players:
            if action_cat is 'punishment':
                updatePlayerScore(player,'punishment',-action.score)
            elif action_cat is 'reward':
                updatePlayerScore(player,'reward',-action.score)
            elif action_cat is 'normal':
                updatePlayerScore(player,'earn',-action.score)
            elif action_cat is 'adjust':
                updatePlayerScore(player,'adjust',-action.score)
        event = models.EventData.get(action.event.key())
        action.delete()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    return HttpResponseRedirect('/admin/events/%s/' %event.key().id())

def overviewClassImage(request):
    players = models.Players.all().count()
    warlock = models.Players.all().filter('player_class','WARLOCK').count()
    mage = models.Players.all().filter('player_class','MAGE').count()
    priest = models.Players.all().filter('player_class','PRIEST').count()
    rogue = models.Players.all().filter('player_class','ROGUE').count()
    druid = models.Players.all().filter('player_class','DRUID').count()
    shaman = models.Players.all().filter('player_class','SHAMAN').count()
    hunter = models.Players.all().filter('player_class','HUNTER').count()
    paladin = models.Players.all().filter('player_class','PALADIN').count()
    warrior = models.Players.all().filter('player_class','WARRIOR').count()
    deadknight = models.Players.all().filter('player_class','DEADKNIGHT').count()
    payload = dict(players=players,
                   warlock=warlock,
                   mage=mage,
                   priest=priest,
                   rogue=rogue,
                   druid=druid,
                   shaman=shaman,
                   hunter=hunter,
                   paladin=paladin,
                   warrior=warrior,
                   deadknight=deadknight,
                   page_type='dkp')
    return render('guildchart.html',payload)

def overviewClassChart(request):
    warlock = models.Players.all().filter('player_class','WARLOCK').count()
    mage = models.Players.all().filter('player_class','MAGE').count()
    priest = models.Players.all().filter('player_class','PRIEST').count()
    rogue = models.Players.all().filter('player_class','ROGUE').count()
    druid = models.Players.all().filter('player_class','DRUID').count()
    shaman = models.Players.all().filter('player_class','SHAMAN').count()
    hunter = models.Players.all().filter('player_class','HUNTER').count()
    paladin = models.Players.all().filter('player_class','PALADIN').count()
    warrior = models.Players.all().filter('player_class','WARRIOR').count()
    deadknight = models.Players.all().filter('player_class','DEADKNIGHT').count()
    
    description = {"class": ("string", "Class"),"count": ("number", "Count"),}
    data = [{"class": "Warlock", "count": warlock},
            {"class": "Mage", "count": mage},
            {"class": "Priest", "count": priest},
            {"class": "Rogue", "count": rogue},
            {"class": "Druid", "count": druid},
            {"class": "Shaman", "count": shaman},
            {"class": "Hunter", "count": hunter},
            {"class": "Paladin", "count": paladin},
            {"class": "Warrior", "count": warrior},
            {"class": "Deadknight", "count": deadknight},]
    
    # Loading it into gviz_api.DataTable
    data_table = gviz_api.DataTable(description)
    data_table.LoadData(data)
    # Creating a JSon string
    json = data_table.ToJSon(columns_order=("class", "count"))
    payload = dict(json=json)
    return render('Vis.html',payload)
    
def overviewScore(request):
    description = {"name": ("string", "Name"),
                   "class": ("string", "Class"),
                   "earned": ("number", "Earned"),
                   "adjust": ("number", "Adjust"),
                   "rewards": ("number", "Rewards"),
                   "punishment": ("number", "Punishment"),
                   "cost": ("number", "Costs"),
                   "current": ("number", "Current"),
                   "rate": ("number", "Rate")}
    player_data_table = memcache.get('player_score_data')
    data_table = gviz_api.DataTable(description)
    data_table.LoadData(player_data_table)
    json = data_table.ToJSon(columns_order=("name", "class","earned","adjust","rewards","punishment","cost","current","rate"))
    payload = dict(json=json)
    return render('PlayersScore.html',payload)

def overviewScore3(request):
    description = {"name": ("string", "Player Name"),
                   "class": ("string", "Player Class"),
                   "earned": ("number", "Earned Score"),
                   }
    player_data_table = memcache.get('player_score_data')
    data_table = gviz_api.DataTable(description)
    data_table.LoadData(player_data_table)
    json = data_table.ToJSon(columns_order=("name","class","earned"))
    payload = dict(json=json,page_type='dkp')
    return render('PlayersScore2.html',payload)

def adminSetCache(request):
    greeting = LoginUrl()
    admin = ChkAdmin()
    if admin == 0:
        greateCache()
    else:
        return HttpResponseRedirect('%s' %users.create_login_url("/"))
    payload = dict()
    return render('memcache.html',payload)

def overviewCache(request):
    dkpTable()
    payload = dict()
    return render('memcache.html',payload)